package com.example.gslih.myapplication.file.impl;

import android.Manifest;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast;

import com.example.gslih.myapplication.MainActivity;
import com.example.gslih.myapplication.R;
import com.example.gslih.myapplication.file.TtFilePathUtil;
import com.example.gslih.myapplication.file.TtFileUtil;
import com.example.gslih.myapplication.http.retrofit.TtRetrofitInterface;
import com.example.gslih.myapplication.log.TtLogUtil;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * create by gene 2018/12/21  17:14
 */
public class FileCreateActivity extends AppCompatActivity {
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.button3)
    Button button3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filepath);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.button)
    public void onButtonClicked() {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                testRetrofit();
                return null;
            }
        }.execute();
    }

    @OnClick(R.id.button2)
    public void onButton2Clicked() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean){
                            String path = TtFileUtil.makeDirs(TtFilePathUtil.getExternalFilesDir(FileCreateActivity.this), "crash/file/log");
                            File file = new File(path,"msg3.txt");
                            boolean isExist = false;
                            if (!file.exists()){
                                isExist = file.createNewFile();
                            }
                            if (isExist){
                                path = true+"  "+path;
                            }else {
                                path=null+"";
                            }

                            button2.setText(path);

                        }
                    }
                })
                ;
//        button2.setText(TtFilePathUtil.getExternalStoragePublicDirectory().toString());
    }

    @OnClick(R.id.button3)
    public void onButton3Clicked() {
    }


    public void testRetrofit(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.gszwfw.gov.cn/media/0/")
                .build();
        TtRetrofitInterface ttRetrofitInterface = retrofit.create(TtRetrofitInterface.class);
        Call<ResponseBody> call = ttRetrofitInterface.call();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                final Response<ResponseBody> responses = response;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (TtFilePathUtil.ifExternalMounted()){
                            String str = "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH";
                            String path = TtFilePathUtil.getExternalFilesDir(FileCreateActivity.this);
                            File file = new File(path+File.separator+"gszwfw.mp3");
                            if (file.exists()){
                                boolean b = file.delete();
                                TtLogUtil.d(b+"");
                            }else {
                                TtLogUtil.d("不存在");
                            }
                            int output;
                            double num = 0;
                            byte[] reader = new byte[8192];
                            long all = responses.body().contentLength();
                            TtLogUtil.d(all+"");
                            try {
                                InputStream inputStream = responses.body().byteStream();
                                FileOutputStream fileOutputStream = new FileOutputStream(file);
                                while ((output = inputStream.read(reader))!=-1){
                                    num += output;
                                    fileOutputStream.write(reader,0,output);
                                    TtLogUtil.d(num/all * 100+"%");
                                    TtLogUtil.d(output+"");
                                }
//                        fileOutputStream.write(str.getBytes());
                                fileOutputStream.flush();
                                fileOutputStream.close();
                                inputStream.close();
                                TtLogUtil.d("create successfully");
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }).start();

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                String s = "";
            }
        });

    }
}
